home *** CD-ROM | disk | FTP | other *** search
- /*
- File: TextEditorUtils.cpp
-
- Contains: TextEditor utility functions & classes
-
- Written by: Steve Smith
-
- Copyright: © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
- */
-
- #ifndef _COMPILERDEFS_
- #include "CompDefs.h"
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _EXCEPT_
- // Exceptions define several important macros (e.g. CHECKENV)
- // which are used in the SOM method dispatch glue. If Except.h
- // is not included early enough, exceptions may not be thrown
- // correctly when returning from a SOM method with "ev" parameter set.
- #include <Except.h>
- #endif
-
- // -- TextEditor Includes --
-
- #ifndef SOM_SampleCode_TextEditor_xh
- #include "TextEditor.xh"
- #endif
-
- #ifndef _TEXTEDITORUTILS_
- #include "TextEditorUtils.h"
- #endif
-
- #ifndef _TEXTEDITORDEF_
- #include "TextEditorDef.h"
- #endif
-
- #ifndef _STDTEXTPROPERTIES_
- #include "StdTextProps.h"
- #endif
-
- #ifndef _Textension_
- #include "Textension.h"
- #endif
-
- // -- OpenDoc Includes --
-
- #ifndef SOM_ODNameSpaceManager_xh
- #include <NmSpcMg.xh>
- #endif
-
- #ifndef SOM_ODObjectNameSpace_xh
- #include "ObjectNS.xh"
- #endif
-
- #ifndef _ODTYPES_
- #include <ODTypes.h>
- #endif
-
- #ifndef SOM_ODPart_xh
- #include <Part.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdProps_defined
- #include <StdProps.xh>
- #endif
-
- #ifndef SOM_ODFrame_xh
- #include <Frame.xh>
- #endif
-
- #ifndef SOM_ODFrameFacetIterator_xh
- #include <FrFaItr.xh>
- #endif
-
- #ifndef SOM_ODCanvas_xh
- #include <Canvas.xh>
- #endif
-
- #ifndef SOM_ODTransform_xh
- #include <Trnsform.xh>
- #endif
-
- #ifndef SOM_ODStorageUnit_xh
- #include <StorageU.xh>
- #endif
-
- #ifndef SOM_ODTranslation_h
- #include <Translt.xh>
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _BNDNSUTL_
- #include <BndNSUtl.h>
- #endif
-
- #ifndef _DOCUTILS_
- #include <DocUtils.h>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdTypes_defined
- #include <StdTypes.xh>
- #endif
-
- #ifndef _INFOUTIL_
- #include <InfoUtil.h>
- #endif
-
- #ifndef _ISOSTR_
- #include <ISOStr.h>
- #endif
-
- #ifndef _EXCEPT_
- #include <Except.h>
- #endif
-
- #ifndef _ODMEMORY_
- #include <ODMemory.h>
- #endif
-
- #ifndef _PASCLSTR_
- #include <PasclStr.h>
- #endif
-
- #ifndef _STORUTIL_
- #include <StorUtil.h>
- #endif
-
- #ifndef _STDTYPIO_
- #include <StdTypIO.h>
- #endif
-
- #ifndef _USERSRCM_
- #include <UseRsrcM.h>
- #endif
-
- #ifndef _BARRAY_
- #include <BArray.h>
- #endif
-
- #ifndef _ODUTILS_
- #include <ODUtils.h>
- #endif
-
- #ifndef _TEMPITER_
- #include <TempIter.h>
- #endif
-
- #ifndef _TEMPOBJ_
- #include <TempObj.h>
- #endif
-
- // --- Macintosh Includes ---
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
-
- #ifndef __CONTROLS__
- #include <Controls.h>
- #endif
-
- #ifndef __WINDOWS__
- #include <Windows.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __FIXMATH__
- #include <FixMath.h>
- #endif
-
- #ifndef __FP__
- #include <fp.h>
- #endif
-
- #ifndef __TEXTUTILS__
- #include <TextUtils.h>
- #endif
-
- #ifndef __OSUTILS__
- #include <OSUtils.h>
- #endif
-
- #ifndef __SCRIPT__
- #include <Script.h>
- #endif
-
- #ifndef __FOLDERS__
- #include <Folders.h>
- #endif
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
-
-
- #pragma segment TextEditorUtils
-
- //====================================================================
- // Utility Functions
- //====================================================================
-
- //--------------------------------------------------------------------
- // GetPartName
- //--------------------------------------------------------------------
-
- ODIText* GetPartName(Environment* ev, ODPart* part, ODType category)
- {
- ASSERT(part != kODNULL, kODErrIllegalNullPartInput);
-
- // Get the root part of the document.
- ODDraft* draft = ODGetDraft(ev,part);
- TempODPart rootPart = ODAcquireRootPartOfDraft(ev, draft);
-
- // Get the part name.
- // If we can't get a valid name for the part, we generate one using
- // the user visible category name of the part's category.
- TempODIText partName = ODGetPOName(ev, part, kODNULL);
-
- // If the part is the root of the document, return the name of the file.
- if ( ODObjectsAreEqual(ev, part, rootPart) )
- {
- ODContainer* container = draft->GetDocument(ev)->GetContainer(ev);
- TempPlatformFile file = GetPlatformFileFromContainer(ev, container);
-
- TempODIText fileName = file->GetName();
-
- // Test the file name against the part name. If the two are equivalent
- // (not equal), then use the part name.
- if ( !NamesAreEquivalent(ev, fileName, partName) )
- {
- // If the names are different, return the file name.
- // (This code transfers the ODIText object to the partName tempobj)
- DisposeIText(partName.DontDelete());
- partName = fileName.DontDelete();
- }
- }
- else
- {
- if ( partName == kODNULL || GetITextStringLength(partName) == 0 )
- {
- ODIText* categoryName;
- ODNameSpaceManager* nsMgr = ODGetSession(ev,part)->GetNameSpaceManager(ev);
-
- // Get the category string from the category name space.
- if ( GetUserCatFromCat(nsMgr, category, &categoryName) )
- {
- // If we successfully retrieved the category user string, return it.
- // (This code transfers the ODIText object to the partName tempobj)
- DisposeIText(partName.DontDelete());
- partName = categoryName;
- }
- else
- {
- #if ODDebug
- // This should never happen. Check NMAP for errors.
- DebugStr("\pCategory NMAP bad, or Preferences corrupted.");
- #else
- THROW(kODErrInvalidNSType);
- #endif
- }
- }
- }
-
- return partName.DontDelete();
- }
-
- //--------------------------------------------------------------------
- // NamesAreEquivalent
- //--------------------------------------------------------------------
-
- ODBoolean NamesAreEquivalent(Environment* ev, ODIText* fileName,
- ODIText* partName)
- {
- Str255 fileStr;
- Str255 partStr;
-
- // If the strings are in different languages, we're done.
- if ( (GetITextScriptCode(fileName) != GetITextScriptCode(partName)) ||
- GetITextLangCode(fileName) != GetITextLangCode(partName) )
- return kODFalse;
-
- GetITextPString(fileName, fileStr);
- GetITextPString(partName, partStr);
-
- // If the string lengths are different, we're done.
- if ( fileStr[0] != partStr[0] )
- return kODFalse;
-
- // Return the Toolbox string equivalence test.
- return EqualString(fileStr,partStr,kODTrue,kODTrue);
- }
-
- //--------------------------------------------------------------------
- // GetEditorScriptLanguage
- //--------------------------------------------------------------------
-
- void GetEditorScriptLanguage(Environment* ev, ODScriptCode* script,
- ODLangCode* language)
- {
- CUsingLibraryResources res;
-
- Handle versHdl = Get1Resource('vers', 1);
-
- // Get the region code of the editor, otherwise use the
- // region code the of the primary system script.
-
- long region;
- if ( versHdl )
- {
- region = (long)(*(VersRecHndl) versHdl)->countryCode;
- ReleaseResource(versHdl);
- }
- else
- {
- region = GetScriptManagerVariable(smRegionCode);
- }
-
- // Spanish & Japanese are not actually supported by the editor.
- // They are provided as examples of how to add recognition of
- // additional regions (see Script.h for region codes).
-
- switch ((short) region)
- {
- case verUS:
- *script = smRoman;
- *language = langEnglish;
- break;
-
- case verSpain:
- *script = smRoman;
- *language = langSpanish;
- break;
-
- case verJapan:
- *script = smJapanese;
- *language = langJapanese;
- break;
-
- default:
- *script = smRoman;
- *language = langEnglish;
- }
- }
-
- //--------------------------------------------------------------------
- // LoadThumbnail
- //--------------------------------------------------------------------
-
- void LoadThumbnail(Environment* ev, Handle* thumbnail)
- {
- if ( *thumbnail ) return;
-
- CUsingLibraryResources res;
- *thumbnail = (Handle) GetPicture(kThumbnailPicture);
- }
-
- //--------------------------------------------------------------------
- // TilePartWindow
- //--------------------------------------------------------------------
-
- Rect TilePartWindow(Environment* ev, Rect* facetBounds, Rect* partWindowBounds)
- {
- const short kWindowTilingConst = 20;
- const short kLeftToRight = 0;
- const short kRightToLeft = -1;
-
- // Get the direction for the primary script system running on this machine.
- // (Right-to-Left or Left-to-Right)
- short direction = GetSysDirection();
-
- // The child window should be tiled from the topLeft corner of the
- // active facet whose frame is being opened.
- if ( direction == kLeftToRight )
- {
- // Position the window rect at the top/left corner of the facet.
- OffsetRect(partWindowBounds, facetBounds->left, facetBounds->top);
- // Now tile the window rect down and to the right.
- OffsetRect(partWindowBounds, kWindowTilingConst, kWindowTilingConst);
- }
- // The child window should be tiled from the topRight corner of the
- // active facet whose frame is being opened.
- else if ( direction == kRightToLeft )
- {
- // Position the window rect at the top/right corner of the facet.
- OffsetRect(partWindowBounds, (partWindowBounds->right - facetBounds->right),
- facetBounds->top);
- // Now tile the window rect down and to the left.
- OffsetRect(partWindowBounds, -kWindowTilingConst, kWindowTilingConst);
- }
-
- return *partWindowBounds;
- }
-
- //--------------------------------------------------------------------
- // CountFacets
- //--------------------------------------------------------------------
-
- ODUShort CountFacets(Environment* ev, ODFrame* frame)
- {
- ODUShort facetCount = 0;
-
- TempODFrameFacetIterator ffiter(ev, frame);
- ODFacet* facet = ffiter.First();
- while ( ffiter.IsNotComplete() )
- {
- facetCount++;
- facet = ffiter.Next();
- }
-
- return facetCount;
- }
-
- //------------------------------------------------------------------------------
- // TranslateData
- //------------------------------------------------------------------------------
-
- void TranslateData( Environment* ev, ODStorageUnit* fromSU, ODStorageUnit* toSU,
- ODType translateKind, ODType desiredKind )
- {
- ODTranslation* translation = toSU->GetSession(ev)->GetTranslation(ev);
-
- // create a view of the data to translate.
- fromSU->Focus(ev, kODPropContents, kODPosUndefined, translateKind, 0, kODPosUndefined);
- TempODStorageUnitView fromView = fromSU->CreateView(ev);
-
- // create a view of the destination for the translated data.
- toSU->AddProperty(ev, kODPropContents);
- toSU->AddValue(ev, desiredKind);
- TempODStorageUnitView toView = toSU->CreateView(ev);
-
- THROW_IF_ERROR(translation->TranslateView(ev, fromView, toView));
- }
-
- //--------------------------------------------------------------------
- // GetIndFINF
- // (must have focused resource fork before calling)
- //--------------------------------------------------------------------
-
- FINF GetIndFINF(short id, short num)
- {
- Handle fh;
- FINF finf;
-
- finf.font = systemFont;
- finf.face = 0;
- finf.size = 0;
-
- if (num > 0)
- if ((fh = GetResource('finf', id)) != nil)
- if (num <= *(short *)(*fh))
- finf = ((FINFPtr)((*fh) + 2))[num - 1];
-
- return finf;
- }
-
- //--------------------------------------------------------------------
- // SetDialogDefaults
- //--------------------------------------------------------------------
-
- void SetDialogDefaults(DialogPtr dialog, ODUShort finfID, ODUShort index)
- {
- FINF finf;
-
- SetDialogDefaultItem(dialog, ok);
- SetDialogCancelItem(dialog, cancel);
-
- if ( finfID != 0 )
- {
- SetPort(dialog);
-
- finf = GetIndFINF(finfID, index);
-
- TextFont(finf.font);
- TextSize(finf.size);
- TextFace(finf.face);
-
- DialogPeek dialogPeek = (DialogPeek) dialog;
- (**(dialogPeek->textH)).txFont = finf.font;
- (**(dialogPeek->textH)).txSize = finf.size;
- (**(dialogPeek->textH)).txFace = finf.face;
-
- FontInfo finfo;
- GetFontInfo(&finfo);
- (**(dialogPeek->textH)).lineHeight = finfo.leading + finfo.ascent + finfo.descent;
- (**(dialogPeek->textH)).fontAscent = finfo.ascent;
-
- TECalText(dialogPeek->textH);
- }
- }
-
- //--------------------------------------------------------------------
- // FixedToIntRect
- //--------------------------------------------------------------------
-
- void FixedToIntRect(ODRect& fixedRect, Rect& intRect)
- {
- intRect.top = FixedToInt(fixedRect.top);
- intRect.left = FixedToInt(fixedRect.left);
- intRect.bottom = FixedToInt(fixedRect.bottom);
- intRect.right = FixedToInt(fixedRect.right);
- }
-
- //--------------------------------------------------------------------
- // IntToFixedRect
- //--------------------------------------------------------------------
-
- void IntToFixedRect(Rect& intRect, ODRect& fixedRect)
- {
- fixedRect.left = ff(intRect.left);
- fixedRect.top = ff(intRect.top);
- fixedRect.right = ff(intRect.right);
- fixedRect.bottom = ff(intRect.bottom);
- }
-
- //--------------------------------------------------------------------
- // LongToFixedRect
- //--------------------------------------------------------------------
-
- void LongToFixedRect(TLongRect& longRect, ODRect& fixedRect)
- {
- fixedRect.left = ff(longRect.left);
- fixedRect.top = ff(longRect.top);
- fixedRect.right = ff(longRect.right);
- fixedRect.bottom = ff(longRect.bottom);
- }
-
- //--------------------------------------------------------------------
- // SetMenuCommandIndString
- //--------------------------------------------------------------------
-
- void SetMenuCommandIndString( Environment* ev,
- ODCommandID command,
- ODSShort resID,
- ODSShort index )
- {
- Str255 menuStr;
- ODGetIndString(menuStr, resID, index);
-
- TempODIText menuText = CreateIText(gGlobals->fEditorsScript,
- gGlobals->fEditorsLanguage,
- menuStr);
- gGlobals->fMenuBar->SetItemString(ev, command, menuText);
- }
-
- //--------------------------------------------------------------------
- // ConvertNumberToPixels
- //--------------------------------------------------------------------
-
- ODUShort ConvertNumStrToPixels(Str255 value)
- {
- ODBoolean metric = IsMetric();
- NumberParts* partsTable;
- NumFormatString formatRec;
- Handle itlHandle;
- long offset;
- long length;
- extended80 extValue;
- long double dblValue;
- Fixed fixValue;
- float oneCentimeter = 2.54e+0;
- short result;
-
- GetIntlResourceTable((ScriptCode)GetScriptManagerVariable(smSysScript),
- smNumberPartsTable, &itlHandle, &offset, &length);
-
- HLock(itlHandle);
- partsTable = (NumberPartsPtr) ((long) (*itlHandle) + offset);
-
- result = StringToFormatRec("\p###.##;-###.##;0", partsTable, &formatRec);
-
- result = StringToExtended((ConstStr255Param) value, &formatRec, partsTable, &extValue);
-
- DisposePtr((Ptr)partsTable);
-
- #ifdef __powerc
- x80told(&extValue, &dblValue);
- fixValue = FloatToFixed(dblValue);
- // convert extended80 to fixed
- #else
- fixValue = FloatToFixed((float_t)extValue);
- #endif
-
- if ( metric ) fixValue = FixDiv(fixValue, FloatToFixed(oneCentimeter));
- // convert centimeters to inches
-
- fixValue = FixMul(fixValue, IntToFixed(72));
- // multiply inches by 72 pixels
-
- HUnlock(itlHandle);
-
- return FixedToInt(fixValue);
- // return pixel value
- }
-
- //--------------------------------------------------------------------
- // ConvertNumberToPixels
- //--------------------------------------------------------------------
-
- void ConvertPixelsToNumStr(ODUShort value, Str255* numStr)
- {
- ODBoolean metric = IsMetric();
- NumberPartsPtr partsTable;
- NumFormatString formatRec;
- Handle itlHandle;
- long offset;
- long length;
- extended80 extValue;
- Fixed fixValue;
- long double dblValue;
- float oneCentimeter = 2.54e+0;
- short result;
-
-
- GetIntlResourceTable((ScriptCode)GetScriptManagerVariable(smSysScript),
- smNumberPartsTable, &itlHandle, &offset, &length);
-
- HLock(itlHandle);
- partsTable = (NumberPartsPtr)((long)(*itlHandle) + offset);
-
- result = StringToFormatRec("\p###.##;-###.##;0", partsTable, &formatRec);
-
- fixValue = FixDiv(IntToFixed(value), IntToFixed(72));
- // convert pixels to inches
-
- if ( metric ) fixValue = FixMul(fixValue, FloatToFixed(oneCentimeter));
- // convert inches to centimeters
-
- #ifdef __powerc
- extValue.man[0] = extValue.man[1] = extValue.man[2] = extValue.man[3] = extValue.man[4] = 0;
- dblValue = (long double) FixedToFloat(fixValue);
- ldtox80(&dblValue, &extValue);
- // convert fixed value to extended80
- #else
- extValue = 0;
- extValue = (extended80) FixedToFloat(fixValue);
- #endif
-
- result = ExtendedToString(&extValue, &formatRec, partsTable, *numStr);
- // create decimal string for display
-
- HUnlock(itlHandle);
- }
-
- //-------------------------------------------------------------------------
- // ScrollProc
- //-------------------------------------------------------------------------
-
- extern pascal void ScrollProc(ControlHandle theControl, short partCode)
- {
-
- if (partCode == 0)
- return;
-
- ScrollDataRec* sData = (ScrollDataRec*)(**theControl).contrlRfCon;
-
- CTextension* textension = sData->textension;
-
- if ( textension == kODNULL )
- return;
-
- Rect viewRect;
- textension->GetViewRect(&viewRect);
-
- ODBoolean vertScrollbar = kODTrue;
- if ( sData->hScrollbar == theControl )
- vertScrollbar = kODFalse;
-
- short delta;
- switch (partCode)
- {
- case kControlUpButtonPart:
- delta = -kSingleScrollUnit;
- break;
-
- case kControlDownButtonPart:
- delta = kSingleScrollUnit;
- break;
-
- case kControlPageUpPart:
- case kControlPageDownPart:
- if ( vertScrollbar )
- delta = (viewRect.bottom - viewRect.top);
- else
- delta = (viewRect.right - viewRect.left);
-
- if ( partCode == kControlPageUpPart )
- delta = -delta;
- break;
-
- default:
- delta = 0;
- }
-
- short vScroll = GetControlValue(sData->vScrollbar);
- short hScroll = GetControlValue(sData->hScrollbar);
-
- SetControlValue(theControl, GetControlValue(theControl) + delta);
-
- vScroll -= GetControlValue(sData->vScrollbar);
- hScroll -= GetControlValue(sData->hScrollbar);
-
- TLongPoint scrollAmount(hScroll, vScroll);
- textension->Scroll(&scrollAmount);
- }
-
- //-------------------------------------------------------------------------
- // ScrollProc
- //-------------------------------------------------------------------------
-
- extern void ClickLoopProc(unsigned short message, void* data, void* refcon)
- {
- if ( message == kAutoScroll)
- {
- TLongPoint scrollAmount = *(TLongPoint*)data;
-
- ScrollDataRec* sData = (ScrollDataRec*)refcon;
-
- SetControlValue(sData->vScrollbar, GetControlValue(sData->vScrollbar) -
- (short) scrollAmount.v);
- SetControlValue(sData->hScrollbar, GetControlValue(sData->hScrollbar) -
- (short) scrollAmount.h);
- }
- }
-
- //-------------------------------------------------------------------------
- // IsNumericCharacter
- //-------------------------------------------------------------------------
-
- static ODBoolean IsNumericCharacter( UInt32 charKeyCode )
- {
- char charCode = charKeyCode & charCodeMask;
- return (charCode >= 0x30 && charCode <= 0x39);
- }
-
- //-------------------------------------------------------------------------
- // OtherDialogFilter
- //-------------------------------------------------------------------------
-
- extern pascal ODBoolean OtherDialogFilter(DialogPtr dialog, EventRecord* event,
- short* itemHit)
- {
- ODBoolean result = kODFalse;
-
- if ( event->what == keyDown ) // just looking for keystrokes
- {
- if ( !IsNumericCharacter(event->message) )
- {
- char charCode = event->message & charCodeMask;
- ODBoolean disallow = kODFalse;
-
- switch ( charCode )
- {
- case 0x0d: // Return
- case 0x03: // Enter
- case 0x1b: // Esc.
- case 0x08: // delete key
- break;
-
- case 0x2e: // '.'
- // We need to allow command-., but disallow just '.':
- if ( (event->modifiers & cmdKey) != cmdKey )
- disallow = kODTrue;
- break;
-
- default:
- // For all non-numeric keystrokes, beep at the user
- // and clear the event so the char is not typed.
- disallow = kODTrue;
- break;
- }
-
- if ( disallow )
- {
- // In order to disallow the entry, beep at the user
- // and clear the event so the char is not typed.
- SysBeep(1);
- event->what = nullEvent;
- }
- }
- }
-
- if ( result == kODFalse )
- {
- // Forward other events to the standard filter-proc:
- ModalFilterUPP proc;
- if ( GetStdFilterProc(&proc) == noErr )
- // Call through to std filter-proc
- result = CallModalFilterProc(proc, dialog, event, itemHit);
- else
- WARN("Couldn't get std filter proc.");
- }
-
- return result;
- }
-
- //-------------------------------------------------------------------------
- // Navigation Key character codes
- //-------------------------------------------------------------------------
-
- const char kKeyLeftArrow = 0x1C;
- const char kKeyRightArrow = 0x1D;
- const char kKeyUpArrow = 0x1E;
- const char kKeyDownArrow = 0x1F;
-
- const char kKeyHome = 0x01;
- const char kKeyEnd = 0x04;
- const char kKeyPageUp = 0x0B;
- const char kKeyPageDown = 0x0C;
-
- //-------------------------------------------------------------------------
- // IsArrowKey
- //-------------------------------------------------------------------------
-
- static ODBoolean IsArrowKey( char charCode )
- {
- switch ( charCode )
- {
- case kKeyLeftArrow:
- case kKeyRightArrow:
- case kKeyUpArrow:
- case kKeyDownArrow:
- return kODTrue;
- }
-
- return kODFalse;
- }
-
- //-------------------------------------------------------------------------
- // IsNavigationKey
- //-------------------------------------------------------------------------
-
- static ODBoolean IsNavigationKey( char charCode )
- {
- if ( IsArrowKey(charCode) )
- return kODTrue;
-
- switch ( charCode )
- {
- case kKeyHome:
- case kKeyEnd:
- case kKeyPageUp:
- case kKeyPageDown:
- return kODTrue;
- }
-
- return kODFalse;
- }
-
- //-------------------------------------------------------------------------
- // KeyCausesModification
- //-------------------------------------------------------------------------
-
- ODBoolean KeyCausesModification( EventRecord* event )
- {
- // Here we are returning that any key other then a navigation key
- // causes the document to be modified.
-
- char charCode = event->message & charCodeMask;
- return !IsNavigationKey(charCode);
- }
-
-
- //=========================================================================
- // CFrameProxy
- //=========================================================================
-
- //-------------------------------------------------------------------------
- // CFrameProxy::SetFrame
- //
- // Description: The code will not affect the proxy fields unless it can
- // successfully acquire the incoming frame, its id, and
- // the draft the frame lives in. If something goes wrong,
- // the proxy remains unchanged.
- //-------------------------------------------------------------------------
-
- void CFrameProxy::SetFrame(Environment* ev, ODFrame* frame)
- {
- ASSERT(frame!=kODNULL, kODErrIllegalNullFrameInput);
-
- // Using the temp object will cleanup the incoming
- // frame's refcount if something goes wrong.
- frame->Acquire(ev);
- TempODFrame tFrame = frame;
-
- // Perform all the operations that can fail, first.
- TempODPart tPart = frame->AcquirePart(ev);
- ODID id = frame->GetID(ev);
- ODDraft* draft = ODGetDraft(ev, tPart);
- ODReleaseObject(ev, fFrame);
-
- // If all went well, set the proxy fields.
- fFrame = tFrame.DontRelease();
- fID = id;
- fDraft = draft;
- }
-
- //-------------------------------------------------------------------------
- // CFrameProxy::GetFrame
- //-------------------------------------------------------------------------
-
- ODFrame* CFrameProxy::GetFrame(Environment* ev)
- {
- if ( fFrame == kODNULL )
- {
- TRY
- TempODFrame frame = fDraft->AcquireFrame(ev, fID);
- this->SetFrame(ev,frame);
- CATCH_ALL
- fFrame = kODNULL;
- fID = kODNULLID;
- ENDTRY
- }
- return fFrame;
- }
-
- //-------------------------------------------------------------------------
- // CFrameProxy::FrameIsLoaded
- //-------------------------------------------------------------------------
-
- ODBoolean CFrameProxy::FrameIsLoaded(Environment* ev)
- {
- ODBoolean loaded = (fFrame != kODNULL ||
- fDraft->IsValidID(ev, fID));
- return loaded;
- }
-
- //-------------------------------------------------------------------------
- // CFrameProxy::Purge
- //-------------------------------------------------------------------------
-
- void CFrameProxy::Purge(Environment* ev)
- {
- if ( fFrame != kODNULL )
- {
- fID = fFrame->GetID(ev);
- ODReleaseObject(ev, fFrame);
- }
- }
-
- //=========================================================================
- // CFrameInfo
- //=========================================================================
-
- //-------------------------------------------------------------------------
- // CFrameInfo::CFrameInfo
- //-------------------------------------------------------------------------
-
- CFrameInfo::CFrameInfo(ODSession* session)
- {
- fSession = session;
- fFrameActive = kODFalse;
- fFrameReactivate = kODFalse;
- fShouldDisposeWindow = kODFalse;
- fActiveFacet = kODNULL;
- fSourceFrame = kODNULL;
- fDependentFrame = kODNULL;
- fPartWindowID = kODNULLID;
- }
-
- //-------------------------------------------------------------------------
- // CFrameInfo::~CFrameInfo
- //-------------------------------------------------------------------------
-
- CFrameInfo::~CFrameInfo()
- {
- // Deleting the proxies will release the encapsulated frames.
- ODDeleteObject(fDependentFrame);
- ODDeleteObject(fSourceFrame);
- }
-
- //-------------------------------------------------------------------------
- // CFrameInfo::Externalize
- //-------------------------------------------------------------------------
-
- void CFrameInfo::Externalize(Environment* ev, ODStorageUnitView* storageUnitView)
- {
- // This method assumes that OpenDoc has passed us a storageUnitView
- // that is focused to a property, but no particular value.
-
- ODStorageUnit* storageUnit = storageUnitView->GetStorageUnit(ev);
-
- this->CleanseFrameInfoProperty(ev, storageUnit);
- this->ExternalizeFrameInfo(ev, storageUnit, kODNULLKey, kODNULL);
- }
-
- //-------------------------------------------------------------------------
- // CFrameInfo::CleanseFrameInfoProperty
- //-------------------------------------------------------------------------
-
- void CFrameInfo::CleanseFrameInfoProperty(Environment* ev, ODStorageUnit* storageUnit)
- {
- ODULong numValues;
- ODULong index;
-
- numValues = storageUnit->CountValues(ev);
-
- for (index = numValues; index >= 1; index--)
- {
- // Index, from 1 to n, through the values.
- storageUnit->Focus(ev, kODNULL, kODPosSame,
- kODNULL, index, kODPosUndefined);
-
- // Get the ISO type name for the value. The temp object
- // will automatically delete the returned value when this
- // scope is exited.
- TempODValueType value = storageUnit->GetType(ev);
-
- // If the value type is not one we support, remove it.
- if ( !ODISOStrEqual(value, kTextEditorPartInfo) )
- storageUnit->Remove(ev);
- }
- }
-
- //-------------------------------------------------------------------------
- // CFrameInfo::ExternalizeFrameInfo
- //-------------------------------------------------------------------------
-
- void CFrameInfo::ExternalizeFrameInfo(Environment* ev, ODStorageUnit* storageUnit,
- ODDraftKey key, ODFrame* scopeFrame)
- {
- // This method behaves much the same way as the ExternalizeStateInfo
- // method.
-
- if ( storageUnit->Exists(ev, kODNULL, kTextEditorPartInfo, 0) )
- {
- // Persistent object references are stored in a side table, rather than
- // in the property/value stream. Thus, deleting the contents of a value
- // will not "delete" the references previously written to that value. To
- // completely "delete" all references written to the value, we must
- // remove the value and add it back.
-
- storageUnit->Focus(ev, kODNULL, kODPosSame, kTextEditorPartInfo, 0, kODPosUndefined);
- storageUnit->Remove(ev);
- }
-
- // Add a value to write the data into.
- storageUnit->AddValue(ev, kTextEditorPartInfo);
-
- // Write a weak reference to our source frame.
- {
- ODStorageUnitRef weakRef = {0,0,0,0};
-
- if ( fSourceFrame )
- {
- ODID frameID = fSourceFrame->GetID();
- ODID scopeFrameID = ( scopeFrame ? scopeFrame->GetID(ev) : kODNULLID );
- ODDraft* fromDraft = fSourceFrame->GetDraft();
-
- // If a draft key exists, then we are being cloned to another draft.
- // We must "weak" clone our display frame and reference the cloned
- // frame. The part re-uses the frameID variable so there aren't two
- // different GetWeakStorageUnitRef calls.
- if ( key )
- frameID = fromDraft->WeakClone(ev, key, frameID, kODNULLID, scopeFrameID);
-
- // Write out weak references to each of the part's display frames.
- storageUnit->GetWeakStorageUnitRef(ev, frameID, weakRef);
- }
- StorageUnitSetValue(storageUnit, ev, sizeof(ODStorageUnitRef), (ODPtr)&weakRef);
- }
-
- // Write a weak reference to our dependent frame.
- {
- ODStorageUnitRef weakRef = {0,0,0,0};
-
- if ( fDependentFrame )
- {
- ODID frameID = fDependentFrame->GetID();
- ODID scopeFrameID = ( scopeFrame ? scopeFrame->GetID(ev) : kODNULLID );
- ODDraft* fromDraft = fDependentFrame->GetDraft();
-
- // If a draft key exists, then we are being cloned to another draft.
- // We must "weak" clone our display frame and reference the cloned
- // frame. The part re-uses the frameID variable so there aren't two
- // different GetWeakStorageUnitRef calls.
- if ( key )
- frameID = fromDraft->WeakClone(ev, key, frameID, kODNULLID, scopeFrameID);
-
- // Write out weak references to each of the part's display frames.
- storageUnit->GetWeakStorageUnitRef(ev, frameID, weakRef);
- }
- StorageUnitSetValue(storageUnit, ev, sizeof(ODStorageUnitRef), (ODPtr)&weakRef);
- }
- }
-
- //-------------------------------------------------------------------------
- // CFrameInfo::CloneInto
- //-------------------------------------------------------------------------
-
- void CFrameInfo::CloneInto(Environment *ev, ODDraftKey key,
- ODStorageUnitView* storageUnitView, ODFrame* scopeFrame)
- {
- // This method assumes that OpenDoc has passed us a storageUnitView
- // that is focused to a property, but no particular value.
-
- ODStorageUnit* storageUnit = storageUnitView->GetStorageUnit(ev);
-
- if ( storageUnit->Exists(ev, kODNULL, kTextEditorPartInfo, 0) == kODFalse )
- {
- this->ExternalizeFrameInfo(ev, storageUnit, key, scopeFrame);
- }
- }
-
- //-------------------------------------------------------------------------
- // CFrameInfo::InitFromStorage
- //------------------------------------------------------------------------------
-
- void CFrameInfo::InitFromStorage(Environment* ev, ODStorageUnitView* storageUnitView)
- {
- // This method assumes that OpenDoc has passed us a storageUnitView
- // that is focused to a property, but no particular value.
-
- ODStorageUnit* storageUnit = storageUnitView->GetStorageUnit(ev);
-
- if ( storageUnit->Exists(ev, kODNULL, kTextEditorPartInfo, 0) )
- {
- TRY
- storageUnit->Focus(ev, kODNULL, kODPosSame,
- kTextEditorPartInfo, 0 , kODPosUndefined);
-
- ODStorageUnitRef weakRef = {0,0,0,0};
- StorageUnitGetValue(storageUnit, ev, sizeof(ODStorageUnitRef),
- (ODPtr)&weakRef);
-
- if ( storageUnit->IsValidStorageUnitRef(ev, weakRef) )
- {
- // Convert the reference into a runtime id.
- ODID frameID = storageUnit->GetIDFromStorageUnitRef(ev, weakRef);
-
- // Create a proxy class to support the lazy internalization.
- CFrameProxy* proxy = new CFrameProxy;
- proxy->InitFrameProxy(frameID, ODGetDraft(ev,storageUnit));
-
- // Store the proxy source frame.
- fSourceFrame = proxy;
- }
-
- CATCH_ALL
- ODDeleteObject(fSourceFrame);
- fSourceFrame = kODNULL;
- ENDTRY
-
- TRY
- ODStorageUnitRef weakRef = {0,0,0,0};
- StorageUnitGetValue(storageUnit, ev, sizeof(ODStorageUnitRef),
- (ODPtr)&weakRef);
-
- if ( storageUnit->IsValidStorageUnitRef(ev, weakRef) )
- {
- // Convert the reference into a runtime id.
- ODID frameID = storageUnit->GetIDFromStorageUnitRef(ev, weakRef);
-
- // Create a proxy class to support the lazy internalization.
- CFrameProxy* proxy = new CFrameProxy;
- proxy->InitFrameProxy(frameID, ODGetDraft(ev,storageUnit));
-
- // Store the proxy dependent frame.
- fDependentFrame = proxy;
- }
-
- CATCH_ALL
- ODDeleteObject(fDependentFrame);
- fDependentFrame = kODNULL;
- ENDTRY
- }
- }
-
- //-------------------------------------------------------------------------
- // CFrameInfo::SetSourceFrame
- //-------------------------------------------------------------------------
-
- void CFrameInfo::SetSourceFrame(Environment* ev, ODFrame* frame)
- {
- if ( frame != kODNULL )
- {
- // Create a proxy class to support the lazy internalization.
- CFrameProxy* proxy = new CFrameProxy;
- proxy->InitFrameProxy(ev,frame);
-
- // Store the proxy source frame after clearing the old one.
- ODDeleteObject(fSourceFrame);
- fSourceFrame = proxy;
- }
- }
-
- //-------------------------------------------------------------------------
- // CFrameInfo::ReleaseSourceFrame
- //-------------------------------------------------------------------------
-
- void CFrameInfo::ReleaseSourceFrame(Environment* ev)
- {
- ODDeleteObject(fSourceFrame);
- }
-
- //-------------------------------------------------------------------------
- // CFrameInfo::SetDependentFrame
- //-------------------------------------------------------------------------
-
- void CFrameInfo::SetDependentFrame(Environment* ev, ODFrame* frame)
- {
- if ( frame != kODNULL )
- {
- // Create a proxy class to support the lazy internalization.
- CFrameProxy* proxy = new CFrameProxy;
- proxy->InitFrameProxy(ev,frame);
-
- // Store the proxy dependent frame after clearing the old one.
- ODDeleteObject(fDependentFrame);
- fDependentFrame = proxy;
- }
- }
-
- //-------------------------------------------------------------------------
- // CFrameInfo::ReleaseDependentFrame
- //-------------------------------------------------------------------------
-
- void CFrameInfo::ReleaseDependentFrame(Environment* ev)
- {
- ODDeleteObject(fDependentFrame);
- }
-
- //-------------------------------------------------------------------------
- // CFrameInfo::AcquirePartWindow
- //-------------------------------------------------------------------------
-
- ODWindow* CFrameInfo::AcquirePartWindow(Environment* ev)
- {
- ODWindow* window = kODNULL;
-
- if ( fPartWindowID )
- {
- window = fSession->GetWindowState(ev)->AcquireWindow(ev, fPartWindowID);
- if ( !window )
- fPartWindowID = kODNULLID;
- }
-
- return window;
- }
-
- //-------------------------------------------------------------------------
- // CFrameInfo::SetPartWindow
- //-------------------------------------------------------------------------
-
- void CFrameInfo::SetPartWindow(Environment* ev, ODWindow* window)
- {
- fPartWindowID = window ? window->GetID(ev) : kODNULLID;
- }
-
-
-
-
-